﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
//using System.Threading.Tasks;
using System.Management;
using Spire.Pdf;
using System.Drawing.Printing;



namespace ComClassLibrary
{
    public class PrinterHelper
    {
        public enum PrinterStatus
        {
            其他状态 = 1,
            未知,
            空闲,
            正在打印,
            预热,
            停止打印,
            打印中,
            离线
        }
        [DllImport("winspool.drv")]
        public static extern bool SetDefaultPrinter(String Name); //调用win api将指定名称的打印机设置为默认打印机

        /// <summary>
        /// 获取打印机的当前状态
        /// </summary>
        /// <param name="PrinterDevice">打印机设备名称</param>
        /// <returns>打印机状态</returns>
        public static PrinterStatus GetPrinterStat(string PrinterDevice)
        {
            PrinterStatus ret=0;
            string path=@"win32_printer.DeviceId='"+PrinterDevice+"'";
            ManagementObject printer=new ManagementObject(path);
            printer.Get();
            ret=(PrinterStatus)Convert.ToInt32(printer.Properties["PrinterStatus"].Value);
            
            return ret;
        }

        public static void PrintFile(string filePath)
        {
            PdfDocument doc = new PdfDocument(filePath);
            doc.Print();
        }

        public static void PrintFile(string filePath, string printerName)
        {
            PdfDocument doc = new PdfDocument(filePath);
            doc.PrintSettings.PrinterName = printerName;
            doc.Print();
        }

        /// <summary>
        /// 设置系统默认打印机
        /// </summary>
        /// <param name="printerName"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public static bool SetSysDefaultPrinter(string printerName, ref string errMsg)
        {
            List<String> fPrinters = new List<String>();
            fPrinters = GetLocalPrinters();
            foreach (var item in fPrinters)
            {
                if (printerName == item)
                {
                    SetDefaultPrinter(printerName);
                    return true;
                }
            }
            errMsg = string.Format("本地未找到打印机{0}", printerName);
            return false;
        }

        private static PrintDocument fPrintDocument = new PrintDocument();
        /// <summary>
        /// 获取本机默认打印机名称
        /// </summary>
        /// <returns></returns>
        public static String DefaultPrinter()
        {
            return fPrintDocument.PrinterSettings.PrinterName;
        }
        /// <summary>
        /// 获取本机所有打印机
        /// </summary>
        /// <returns></returns>
        public static List<String> GetLocalPrinters()
        {
            List<String> fPrinters = new List<String>();
            fPrinters.Add(DefaultPrinter()); //默认打印机始终出现在列表的第一项
            foreach (String fPrinterName in PrinterSettings.InstalledPrinters)
            {
                if (!fPrinters.Contains(fPrinterName))
                {
                    fPrinters.Add(fPrinterName);
                }
            }
            return fPrinters;
        }
    }

}
